package cfca.seal.dao;

import java.util.List;
import java.util.Map;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

public class SmartDao
{
  protected JdbcTemplate jdbcTemplate;

  public int getCount(String sql)
  {
    return this.jdbcTemplate.queryForInt(sql);
  }

  public JdbcTemplate getJdbcTemplate() {
    return this.jdbcTemplate;
  }

  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }

  public int getCount(String sql, Object[] args) {
    return this.jdbcTemplate.queryForInt(sql, args);
  }

  public long getLongCount(String sql) {
    return this.jdbcTemplate.queryForLong(sql);
  }

  public long getLongCount(String sql, Object[] args) {
    return this.jdbcTemplate.queryForLong(sql, args);
  }

  public <T> T getBean(Class<T> clazz, String sql) {
    try {
      return this.jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(clazz)); } catch (EmptyResultDataAccessException e) {
    }
    return null;
  }

  public <T> T getBean(Class<T> clazz, String sql, Object[] args)
  {
    try {
      return this.jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(clazz), args); } catch (EmptyResultDataAccessException e) {
    }
    return null;
  }

  public <T> T getBean(Class<T> clazz, String sql, Map<String, ?> args)
  {
    try {
      return this.jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(clazz), new Object[] { args }); } catch (EmptyResultDataAccessException e) {
    }
    return null;
  }

  public <T> List<T> getList(Class<T> clazz, String sql)
  {
    return this.jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(clazz));
  }

  public <T> List<T> getList(Class<T> clazz, String sql, Object[] args) {
    return this.jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(clazz), args);
  }

  public <T> List<T> getList(Class<T> clazz, String sql, Map<String, String> args) {
    return this.jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(clazz), new Object[] { args });
  }

  public int update(String sql) {
    return this.jdbcTemplate.update(sql);
  }

  public int update(String sql, Object[] args) {
    return this.jdbcTemplate.update(sql, args);
  }

  public int update(String sql, Object obj) {
    return this.jdbcTemplate.update(sql, new Object[] { new BeanPropertySqlParameterSource(obj) });
  }

  public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
    return this.jdbcTemplate.batchUpdate(sql, batchArgs);
  }
}